package com.hejia.bulbul.controller;

import org.springframework.beans.BeansException;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.hejia.alauda.service.CommonServiceImpl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Johnny(johnny@hejiajinrong.com) on 14-9-16.
 */
public class BaseController implements ApplicationContextAware {

    protected String STATUS_SUCCESS = "success";
    protected String STATUS_FAILD = "faild";

    @InitBinder
    //此处的参数也可以是ServletRequestDataBinder类型
    public void initBinder(WebDataBinder binder) throws Exception {
        //注册自定义的属性编辑器
        //1、日期
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustomDateEditor dateEditor = new CustomDateEditor(df, true);
        //表示如果命令对象有Date类型的属性，将使用该属性编辑器进行类型转换
        binder.registerCustomEditor(Date.class, dateEditor);

    }

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Assert.notNull(applicationContext);
		CommonServiceImpl.applicationContext = applicationContext;
		Assert.notNull(CommonServiceImpl.applicationContext);
	}
}
